#include "resetpassword.h"
#include"loginWidget.h"


resetPassWord::resetPassWord(QWidget *parent) : QWidget(parent)
{
    this->setStyleSheet("background-color:white;");
    this->setWindowTitle("找回密码");
    init();
    getConnect();
}

void resetPassWord::init()
{
    mainWidget = new QWidget(this);
    mainWidget->setAttribute(Qt::WA_NoSystemBackground);
    mainLayout = new QVBoxLayout(mainWidget);
//    this->setFixedSize(400,355);
    this->setFixedSize(400,365);

    //账号
    QWidget *userWidget = new QWidget;
    userWidget->setFixedSize(380,50);
    userWidget->setStyleSheet(
                    "background-color:white;"
                    "font-size:20px;"
                );
    userLayout = new QHBoxLayout(userWidget);
    user = new QLabel(this);
    user->setStyleSheet("margin-top:2px;");
    user->setText("输入管理员账号");
    userEdit = new QLineEdit(this);
    userEdit->setFixedHeight(35);
    userEdit->setMaxLength(8);
    userLayout->addWidget(user);
    userLayout->addWidget(userEdit);

    //姓名
    QWidget *nameWidget = new QWidget;
    nameWidget->setFixedSize(380,50);
    nameWidget->setStyleSheet(
                    "background-color:white;"
                    "font-size:20px;"
                );
    nameLayout = new QHBoxLayout(nameWidget);
    name = new QLabel(this);
    name->setStyleSheet("margin-top:2px;");
    name->setText("输入管理员姓名");
    nameEdit = new QLineEdit(this);
    nameEdit->setFixedHeight(35);
    nameEdit->setMaxLength(4);
    nameLayout->addWidget(name);
    nameLayout->addWidget(nameEdit);

    //手机号
    QWidget *phoneWidget = new QWidget;
    phoneWidget->setFixedSize(380,50);
    phoneWidget->setStyleSheet(
                    "background-color:white;"
                    "font-size:20px;"
                );
    phoneLayout = new QHBoxLayout(phoneWidget);
    phone = new QLabel(this);
    phone->setStyleSheet("margin-top:2px;");
    phone->setText("输入注册手机号");
    phoneEdit = new QLineEdit(this);
    phoneEdit->setFixedHeight(35);
    phoneEdit->setMaxLength(11);
    phoneLayout->addWidget(phone);
    phoneLayout->addWidget(phoneEdit);

    //密码
    QWidget *pwdWidget = new QWidget;
    pwdWidget->setFixedSize(380,50);
    pwdWidget->setStyleSheet(
                    "background-color:white;"
                    "font-size:20px;"
                );
    pwdLayout = new QHBoxLayout(pwdWidget);
    pwd = new QLabel(this);
    pwd->setStyleSheet("margin-top:2px;");
    pwd->setText("更改管理员密码");
    pwdEdit = new QLineEdit(this);
    pwdEdit->setFixedHeight(35);
    pwdEdit->setMaxLength(16);

    pwdLayout->addWidget(pwd);
    pwdLayout->addWidget(pwdEdit);


    //确认密码
    QWidget *repeatPwdWidget = new QWidget;
    repeatPwdWidget->setFixedSize(380,50);
    repeatPwdWidget->setStyleSheet(
                    "background-color:white;"
                    "font-size:20px;"
                );
    repeatPwdLayout = new QHBoxLayout(repeatPwdWidget);
    repeatPwd = new QLabel(this);
    repeatPwd->setStyleSheet("margin-top:2px;");
    repeatPwd->setText("确认更改新密码");
    repeatPwdEdit = new QLineEdit(this);
    repeatPwdEdit->setFixedHeight(35);
    repeatPwdEdit->setMaxLength(16);

    repeatPwdLayout->addWidget(repeatPwd);
    repeatPwdLayout->addWidget(repeatPwdEdit);


    submitBtn = new QPushButton("验证",this);
    submitBtn->setFixedSize(370,40);
    submitBtn->setStyleSheet(
                        "QPushButton{"
                                "margin-left:10px;"
                                "background-repeat: no-repeat;"
                                "background-color: rgb(54, 99, 254);"
                                "color:white;"
                                "font-size:15px;"
                                "font: 75 14pt 'Microsoft YaHei UI';"
                        "}"
                    );
    connect(submitBtn,&QPushButton::clicked,this,&resetPassWord::on_closeBtn_released);

    //错误提示文本
    msg = new QLabel;
    //设置对齐方式：居中对齐
    msg->setFixedSize(400,40);
    msg->setAlignment(Qt::AlignCenter);
    msg->setStyleSheet("QLabel{"
                            "color:red;"
                            "padding-top:5px;"
                            "background-color:white;"
                            "font-size:15px;"
                            "padding-bottom:20px;"
                       "}");
    mainLayout->addWidget(userWidget);
    mainLayout->addWidget(nameWidget);
    mainLayout->addWidget(phoneWidget);
    mainLayout->addWidget(pwdWidget);
    mainLayout->addWidget(repeatPwdWidget);
    mainLayout->addWidget(submitBtn);
    mainLayout->addWidget(msg);

}
//数据库连接
void resetPassWord::getConnect(){
    //数据库连接
    db = QSqlDatabase::addDatabase("QMYSQL","resetConnection");
    db.setHostName("127.0.0.1");
    db.setPort(3306);
    db.setDatabaseName("IMA_System");
    db.setUserName("root");
    db.setPassword("123456");
    if(db.open()){
        qDebug()<<"数据库连接成功";
    }else{
        qDebug()<<"数据库连接失败"<<"失败原因："<<db.lastError().text();
    }
}

void resetPassWord::on_closeBtn_released(){
    userStr = userEdit->text();
    nameStr = nameEdit->text();
    phoneStr = phoneEdit->text();
    pwdStr = pwdEdit->text();
    repeatPwdStr = repeatPwdEdit->text();

    //创建数据库操作指针并关联数据库
    QSqlQuery query(db);
    query.prepare("SELECT * FROM USER WHERE  username = :userStr AND admin_name = :nameStr AND admin_phone = :phoneStr;");
    query.bindValue(":userStr", userStr);
    query.bindValue(":nameStr", nameStr);
    query.bindValue(":phoneStr", phoneStr);

    // 执行查询操作
    if (query.exec()) {
        if (query.next()) {
            if(pwdStr != repeatPwdStr){
//                QString message=QString("<h2 style='color:rgb(69,198,214)'>")+"密码输入不一致！<br/>";
//                QMessageBox::warning(nullptr, "错误", message);
                msg->setText("密码输入不一致！");
                pwdEdit->clear();
                repeatPwdEdit->clear();
                QTimer *timer = new QTimer(this);
                timer->start(1000);
                connect(timer, &QTimer::timeout, [timer,this]() {
                    msg->setText("");
                    timer->deleteLater();
                });
            }else{
                //nameEdit->clear();
                //phoneEdit->clear();
                query.prepare("UPDATE USER SET PASSWORD = :password,remember_pwd = '0' WHERE username = :username;");
                query.bindValue(":password", repeatPwdStr);
                query.bindValue(":username", userStr);
                if(query.exec()){
//                    QString message=QString("<h2 style='color:rgb(69,198,214)'>")+"密码更改成功！<br/>";
//                    QMessageBox::warning(nullptr, "提示", message);
                    msg->setText("");
                    db.close();
                    this->close();
                }
            }
        } else {

//            QString message=QString("<h2 style='color:rgb(69,198,214)'>")+"管理员信息有误！<br/>";
//            QMessageBox::warning(nullptr, "错误", message);
            msg->setText("管理员信息有误！");
            userEdit->clear();
            nameEdit->clear();
            phoneEdit->clear();
            QTimer *timer = new QTimer(this);
            timer->start(1000);
            connect(timer, &QTimer::timeout, [timer,this]() {
                msg->setText("");
                timer->deleteLater();
            });
        }
    } else {

        QMessageBox::critical(this, "错误", "数据库查询失败");
    }


}

